FROM openfaas/of-watchdog:0.7.2 as watchdog
FROM golang:1.13.1-alpine3.10 as build

RUN apk --no-cache add git
COPY --from=watchdog /fwatchdog /usr/bin/fwatchdog
RUN chmod +x /usr/bin/fwatchdog

ENV CGO_ENABLED=0

RUN mkdir -p /go/src/handler
WORKDIR /go/src/handler
COPY . .

# Run a gofmt and exclude all vendored code.
RUN test -z "$(gofmt -l $(find . -type f -name '*.go' -not -path "./vendor/*" -not -path "./function/vendor/*"))" || { echo "Run \"gofmt -s -w\" on your Golang code"; exit 1; }

ARG GO111MODULE="off"
ARG GOPROXY=""

RUN go build --ldflags "-s -w" -a -installsuffix cgo -o handler .
RUN go test handler/... -cover

FROM harbor.junengcloud.com/openfaas/faas-cli-runner:latest
WORKDIR /home/app
COPY run.sh .
COPY faas-cli-cc .
COPY .gitconfig  /root/
COPY .git-credentials  /root/
RUN chmod +x faas-cli-cc
COPY --from=build /go/src/handler/handler    .
COPY --from=build /usr/bin/fwatchdog         .

USER app

ENV fprocess="./handler"
ENV mode="streaming"
ENV upstream_url="http://127.0.0.1:8082"

CMD ["./fwatchdog"]
